import { GeneralConfig } from '../config';
import { BrowserStorage } from '../storage';

export class RememberMeService {
  // The boolean of user selection.
  // When true the token data will be saved in local storage.
  // When false the token data will be saved in session storage.
  private static rememberMeField: boolean | null = null;

  static get rememberMe(): boolean {
    if (!this.rememberMeField) {
      this.loadRememberMe();
    }

    return this.rememberMeField!;
  }

  static set rememberMe(value: boolean) {
    this.rememberMeField = value;
    BrowserStorage.saveLocalData(GeneralConfig.localRememberMeStorageKey, value);
  }

  /**
   * Loads rememberMe property value.
   */
  private static loadRememberMe(): void {
    // Tries to load from the local storage.
    // If there is no such value in local storage we will assign a defalut value.
    this.rememberMeField =
      (BrowserStorage.loadLocalData(GeneralConfig.localRememberMeStorageKey) as boolean) ?? false;
  }
}
